import numpy as np

__all__ = ["permute"]


def permute(a):
    """
    Creates all unique combinations of a list a that is passed in.
    Function is based off of a function written by John Lettman:
    TCHS Computer Information Systems.  My thanks to him.
    """

    a.sort()  # Sort.

    ## Output the first input sorted.
    yield list(a)

    i = 0
    first = 0
    alen = len(a)

    ## "alen" could also be used for the reference to the last element.

    while True:
        i = alen - 1

        while True:
            i -= 1  # i--

            if a[i] < a[(i + 1)]:
                j = alen - 1

                while not (a[i] < a[j]):
                    j -= 1  # j--

                a[i], a[j] = a[j], a[i]  # swap(a[j], a[i])
                t = a[(i + 1) : alen]
                t.reverse()
                a[(i + 1) : alen] = t

                # Output current.
                yield list(a)

                break  # next.

            if i == first:
                a.reverse()

                # yield list(a)
                return
